$(function () {
    /* 1-获取用户的基本信息 */
    getuserInfo();
    function getuserInfo() {
        $.ajax({
            method:'get',
            url:'/my/userinfo',
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message)
                }
                renderAvatar(res.data);
            }
        })
    }

    /* 2-渲染函数 */
    function renderAvatar(user) {
        var name = user.nickname || user.username;
        $('#welcome').html('欢迎' + name);
        if (user.user_pic) {
            $('.layui-nav-img').prop('src',user.user_pic)
            $('.text-avatar').hide();
        } else {
            var first = name[0];
            first = first.toUpperCase();
            $('.text-avatar').html(first);
            $('.layui-nav-img').hide();
        }
    }

    /* 3-退出登录 */
    $('#logout').on('click', function () {
        layer.confirm('您确定要退出吗?',{icon:3,title:'提示'},function(index) {
            localStorage.removeItem('token');  
            location.href = 'login.html';
            layer.close(index);
        })
    })
})